<?php


class ListingMethodsComponent extends Object {

/******************************************************************************/

var $components = array(
	'Session',
	'RequestHandler',
	'Admin.AutomateMethods'
);

/******************************************************************************/

var $Controller = NULL;

var $settings = array();

/******************************************************************************/

var $scopeConditions = array();

var $paginateLimit = 30;

/******************************************************************************/

function initialize(&$Controller, $settings = array()) {
	$this->Controller = &$Controller;
	$this->settings = (array)$settings;
}

function startup(&$Controller) {
	$ctrl = &$this->Controller;
	// pageCaption
	$string_id = $ctrl->params['controller'] . '_caption';
	$controllerCaption = __($string_id, TRUE);
	if ($controllerCaption !== $string_id) {
		if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && ($ctrl->Model->isTree() || $ctrl->Model->hasRubric())) {
			$string_id = $ctrl->params['action'] . '_rubric_caption';
		} else {
			$string_id = $ctrl->params['action'] . '_caption';
		}
		$actionCaption = __($string_id, TRUE);
		if ($actionCaption !== $string_id) {
			$ctrl->PageTitle->pageCaption = preg_replace(array('~\{action\}~i' /* <<< deprecated */ , '~\{action_caption\}~i'), array($actionCaption, $actionCaption), $controllerCaption);
			$ctrl->PageTitle->pageCaption = preg_replace('~\{caption\}~i', '{data_caption}', $ctrl->PageTitle->pageCaption); // deprecated / legacy fix
		}
	}
}

/******************************************************************************/

function filter($parent_or_rubric_id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated()) {
		if (!isset($ctrl->data[$ctrl->Model->name]) || !is_array($ctrl->data[$ctrl->Model->name])) {
			$ctrl->data[$ctrl->Model->name] = array();
		}
		// merge
		if (isset($ctrl->params['named']['_merge']) && (bool)$ctrl->params['named']['_merge']) {
			$params_named = array_intersect_key($ctrl->params['named'], $ctrl->Model->schema());
			$ctrl->data[$ctrl->Model->name] = Set::merge($params_named, $ctrl->data[$ctrl->Model->name]);
		}
		// process
		if ($this->RequestHandler->isPost()) {
			if ((bool)count($ctrl->data[$ctrl->Model->name] = array_filter(array_map('trim', $ctrl->data[$ctrl->Model->name]), 'strlen'))) { // trim & filter
				$this->Session->write('AdminFilter.' . $ctrl->Model->name, $ctrl->data[$ctrl->Model->name]);
			} elseif ($this->Session->check('AdminFilter.' . $ctrl->Model->name)) {
				$this->Session->delete('AdminFilter.' . $ctrl->Model->name);
			}
			if (isset($ctrl->params['form']['_close']) && (bool)$ctrl->params['form']['_close']) {
				$ctrl->redirect(array('action' => 'index', $parent_or_rubric_id), CODE_ACCEPTED);
			} else {
				$ctrl->redirect(array('action' => 'filter', $parent_or_rubric_id), CODE_ACCEPTED);
			}
		} else {
			$ctrl->set('parent_or_rubric_id', $parent_or_rubric_id);
			if ($ctrl->Model->isTree()) {
				$ctrl->set('parent_id', $parent_id = $parent_or_rubric_id);
			} elseif ($ctrl->Model->hasRubric()) {
				$ctrl->set('rubric_id', $rubric_id = $parent_or_rubric_id);
			}
			$ctrl->set('row', $row = $ctrl->data = array($ctrl->Model->name => (array)$this->Session->read('Filter.' . $ctrl->Model->name)));
			$ctrl->set('field_schema', $field_schema = $ctrl->Model->getFieldSchema(TRUE));
			// render
			if ($ctrl->autoRender && $ctrl->automateRender) {
				$ctrl->render();
			}
		}
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

function setRubricRow($rubric_id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->RubricModel) && $ctrl->RubricModel->isAutomated() && $ctrl->RubricModel->idExists($rubric_id)) {
		$ctrl->set('rubric_id', $rubric_id);
		$ctrl->set('rubric_row', $rubric_row = $ctrl->RubricModel->find('byid', array('value' => $rubric_id)));
		// caption
		if (isset($rubric_row[$ctrl->RubricModel->name][$ctrl->RubricModel->displayField])) {
			$ctrl->PageTitle->dataCaption = $rubric_row[$ctrl->RubricModel->name][$ctrl->RubricModel->displayField];
		}
		return $rubric_row;
	}
	return FALSE;
}

function buildRowsQueryFilter($parent_or_rubric_id = NULL, $flat_list = TRUE) {
	$ctrl = &$this->Controller;
	$query = array('conditions' => array());
	foreach ((array)$this->Session->read('AdminFilter.' . $ctrl->Model->name) as $filter_name => $filter_value) {
		if (($filter_name === 'x-new-row') && $ctrl->Model->hasField('created') && $ctrl->Model->hasField('modified') && ctype_digit($filter_value)) {
			$query['conditions'][] = $ctrl->Model->escapeField('created') . ((bool)(int)$filter_value ? ' = ' : ' <> ') . $ctrl->Model->escapeField('modified');
		} elseif (($filter_name === $ctrl->Model->displayField) && $ctrl->Model->hasField($filter_name) && !empty($filter_value)) {
			$query['conditions'][$ctrl->Model->escapeField($filter_name) . ' LIKE'] = '%' . trim($filter_value) . '%';
		} elseif ($ctrl->Model->hasField($filter_name) && ($ctrl->Model->getColumnType($filter_name) === 'boolean') && ctype_digit($filter_value)) {
			$query['conditions'][$ctrl->Model->escapeField($filter_name)] = (bool)(int)$filter_value;
		}
	}
	return $query;
}

function buildRowsQuery($parent_or_rubric_id = NULL, $flat_list = TRUE) {
	$ctrl = &$this->Controller;
	$query = array('conditions' => array());
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated()) {
		if ($ctrl->Model->isTree() && $flat_list) {
			$query['conditions'][$ctrl->Model->escapeField($ctrl->Model->getParentKey())] = $parent_or_rubric_id;
		} elseif ($ctrl->Model->hasRubric() && is_object($ctrl->RubricModel) && $ctrl->RubricModel->isAutomated()) {
//$query['conditions'][$ctrl->Model->escapeField($ctrl->Model->getRubricKey())] = $parent_or_rubric_id;
if ($ctrl->RubricModel->idExists($parent_or_rubric_id)) {
$query['conditions'][$ctrl->Model->escapeField($ctrl->Model->getRubricKey())] = $parent_or_rubric_id;
}
		}
		if ($ctrl->Model->hasItems() && is_object($ctrl->ItemModel) && $ctrl->ItemModel->isAutomated()) {
/*$query['fields'] = array($ctrl->Model->name . '.*', 'COUNT(ItemCnt.id) AS num_items');
$query['joins'][] = array(
'table' => $ctrl->ItemModel->tablePrefix . $ctrl->ItemModel->table,
'alias' => 'ItemCnt',
'type' => 'LEFT',
'foreignKey' => FALSE,
'conditions' => array('ItemCnt.' . $ctrl->ItemModel->getRubricKey() . ' = ' . $ctrl->Model->escapeField('id'))
);
$query['group'] = 'ItemCnt.' . $ctrl->ItemModel->getRubricKey();*/
		}
		if ($ctrl->Model->isAttachment() && isset($ctrl->params['named']['model'], $ctrl->params['named']['foreign_key'])) {
			$query['conditions'][$ctrl->Model->escapeField('model')] = $ctrl->params['named']['model'];
			$query['conditions'][$ctrl->Model->escapeField('foreign_key')] = $ctrl->params['named']['foreign_key'];
		}
		$query['contain'] = $ctrl->Model->buildContain(1);
	}
	return $query;
}

function setRows($parent_or_rubric_id = NULL, $query = array(), $merge_query = TRUE, $flat_list = TRUE) {
	$ctrl = &$this->Controller;
	if ($merge_query) {
		$query = Set::merge($this->buildRowsQuery($parent_or_rubric_id, $flat_list), $this->buildRowsQueryFilter($parent_or_rubric_id, $flat_list), (array)$this->scopeConditions, (array)$query);
	}
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated()) {
		$ctrl->set('total_rows', $total_rows = $ctrl->Model->find('count'));
		$ctrl->set('count_rows', $count_rows = $ctrl->Model->find('count', $query));
		if ($ctrl->Model->isTree() && !$flat_list) {
			$ctrl->set('paginated', FALSE);
			$ctrl->set('rows', $rows = $ctrl->data = $ctrl->Model->find('threaded', $query));
		} else {
			if ((bool)$this->paginateLimit) {
				$ctrl->paginate[$ctrl->Model->name] = $query;
				$ctrl->paginate[$ctrl->Model->name]['limit'] = $this->paginateLimit;
				$ctrl->set('paginated', TRUE);
				$ctrl->set('paginate_limit', $this->paginateLimit);
				$ctrl->set('rows', $rows = $ctrl->data = $ctrl->paginate($ctrl->Model->name/*, $query['conditions']*/));
			} else {
				$ctrl->set('paginated', FALSE);
				$ctrl->set('rows', $rows = $ctrl->data = $ctrl->Model->find('all', $query));
			}
		}
		return $rows;
	}
	return FALSE;
}

/******************************************************************************/

function index($parent_or_rubric_id = NULL, $query = array(), $merge_query = TRUE, $flat_list = TRUE) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated()) {
		$ctrl->set('parent_or_rubric_id', $parent_or_rubric_id);
		if ($ctrl->Model->isTree() && $flat_list) {
			$ctrl->set('parent_id', $parent_id = $parent_or_rubric_id);
$ctrl->PageTitle->dataCaption = Set::combine($ctrl->Model->getPath($parent_id), '{n}.' . $ctrl->Model->name . '.' . $ctrl->Model->primaryKey, '{n}.' . $ctrl->Model->name . '.' . $ctrl->Model->displayField);
		} elseif ($ctrl->Model->hasRubric() && is_object($ctrl->RubricModel) && $ctrl->RubricModel->isAutomated()) {
			$ctrl->set('rubric_id', $rubric_id = $parent_or_rubric_id);
//if (!$ctrl->RubricModel->idExists($rubric_id)) {
//$ctrl->redirect($ctrl->RubricModel->getIndexURL());;
//}
//$rubric_row = $this->setRubricRow($rubric_id);
if ($ctrl->RubricModel->idExists($rubric_id)) {
$rubric_row = $this->setRubricRow($rubric_id);
}
		}
		$rows = $this->setRows($parent_or_rubric_id, $query, $merge_query, $flat_list);
		// render
		if ($ctrl->autoRender && $ctrl->automateRender) {
			$ctrl->render();
		}
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

}


?>